import { Button, Dialog, Paper, TextField } from "@mui/material";
import { useBoolean, useMemoizedFn, useRequest, useSafeState, useSetState } from "ahooks";
import style from './_index.module.scss';
import { headFileExist, uploadFile, constructBCviz, getFile } from "@/pages/BCViz_new/api";
import { idbCommonArgs } from "@/pages/BCviz/FileUploadSimple";
import { waitMiddleEventLoop } from "@/utils";
import { createOrAddIdb } from "@/utils/idb";
import { str } from "crc-32";
import { useNavigate } from "react-router";
import { every, isUndefined, map, some } from "lodash";
import { setSearchParamForDataset, setStateOnChange } from "../utils";
import { TextFieldProps_Number } from "@/pages/BCViz_new/const";
import { commonUseRequestParams } from "@/utils/const";
import Loading from "../Loading";
import { toast } from "react-toastify";
import { useSearchParams } from "react-router-dom";
import { unstable_batchedUpdates } from "react-dom";
import type { OriginGraphDataReadonlyArr } from "@/pages/BCviz/types";
import ConfirmComp, { getFileNameBEWithExt, getWillResolveData, graphDataToText } from "./ComfirmComp";
const { abs } = Math;
export interface ConfirmModalProps {
  // readonly text: string;
  readonly fileName: string;
  readonly graphData: OriginGraphDataReadonlyArr;
}

export default function ConfirmModal (props: ConfirmModalProps) {
  const { graphData } = props;
  const [isOpen, { setTrue, setFalse }] = useBoolean(false);
  const [, setSearchParams] = useSearchParams();
  const useHash = useSafeState<string>();
  const [, setHash] = useHash;
  return <><Dialog open={isOpen} onClose={() => {
    setFalse();
  }}>
    <ConfirmComp {...props} useHash={useHash} />

  </Dialog>
    <Button fullWidth variant="contained" size="large"
      onClick={() => unstable_batchedUpdates(() => {
        const text = graphDataToText(graphData);
        const hash: string = abs(str(text)).toString(36);
        setHash(hash);
        const fileNameBEWithExt = getFileNameBEWithExt(hash);
        setSearchParamForDataset(setSearchParams)(fileNameBEWithExt);
        setTrue();
        createOrAddIdb({
          ...idbCommonArgs, data: getWillResolveData({ fileNameBEWithExt, text }), IDBValidKey: fileNameBEWithExt
        });
      })}
      disabled={graphData.length === 0}

    >Next Step</Button>
  </>;
}